package org.hugh.space.web.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hugh.space.business.CurrentUser;
import org.hugh.space.entity.FileEntity;
import org.hugh.space.entity.UserEntity;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;


public class FileListServlet  extends BaseSpringServlet{
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException {
		perform(request,response);
	}
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException {
		perform(request,response);
	}
	
	public void perform(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
		
		request.setAttribute("files", getFiles());
		request.getRequestDispatcher("/WEB-INF/jsp/fileList.jsp").forward(request, response);
	}
	
	protected List<FileEntity> getFiles(){
		UserEntity user = CurrentUser.getInstance();
		if(user.isAdmin())
			return  getDao().getFileDao().getAll();
		return getDao().getFileDao().getByUser(CurrentUser.getInstance().getId()+"");
	}

}
